<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once ('api/IDictionary.php');

/**
 *
 * Default implementation of IDictionary
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class SimpleDictionary implements IDictionary
{

  /**
   * Holds Dictionary name/value pairs
   *
   * @var array
   */
  protected $data = array();

  /**
   * Constructor.
   *
   * @param mixed $data initial data to populate the Dictionary
   * @return void
   */
  public function __construct($data = null)
  {
    $this->reset($data);
  }

  /**
   * Automagically calls Dictionary::get($name)
   *
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  public function __get($name)
  {
    $args = func_get_args();
    return call_user_func_array(array($this, 'get'), $args);

   //return $this->get($name);
  }

  /**
   * Automagically calls Dictionary::set($name, $value)
   *
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $data)
   */
  public function __set($name, $value)
  {
    return $this->set($name, $value);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#copyAll($data)
   */
  protected function _copyAll($data)
  {
    if (empty($data)) {
      return;
    }

    if (is_a($data, 'IDictionary')) {
      $data = $data->getAll();
    }

    foreach ($data as $name => $value) {
      $this->set($name, $value);
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  protected function _del($name)
  {
    if (isset($this->data[$name])) {
      unset($this->data[$name]);
    }

    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  protected function _get($name)
  {
    if (isset($this->data[$name])) {
      return $this->data[$name];
    }
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  protected function _has($name)
  {
    return isset($this->data[$name]);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  protected function _set($name, $value)
  {
    $this->data[$name] = $value;
    return $this;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#copyAll($data)
   */
  public function copyAll($data)
  {
    $args = func_get_args();
    return call_user_func_array(array($this, '_copyAll'), $args);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#del($name)
   */
  public function del($name)
  {
    return $this->_del($name);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#get($name)
   */
  public function get($name)
  {
    //return $this->_get($name);
    $args = func_get_args();
    return call_user_func_array(array($this, '_get'), $args);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#getAll()
   */
  public function getAll()
  {
    return $this->data;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#getNames()
   */
  public function getNames()
  {
    $names = array_keys($this->data);
    return $names;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#has($name)
   */
  public function has($name)
  {
    return $this->_has($name);
  }

  /**
   * Create Dictionary from $data
   *
   * If $data is already a Dictionary, it is returned unchanged.
   *
   * @param mixed $data
   * @return Dictionary
   */
  public static function parseFrom($data)
  {
    if (! is_a($data, 'IDictionary')) {
      $data = new SimpleDictionary($data);
    }

    return $data;
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#reset($data)
   */
  public function reset($data = null)
  {
    $this->data = array();
    return $this->copyAll($data);
  }

  /**
   * (non-PHPdoc)
   * @see simplify/kernel/data/api/IDictionary#set($name, $value)
   */
  public function set($name, $value)
  {
    return $this->_set($name, $value);
  }

  /**
   * Get $this->data
   *
   * @return array
   */
  public function &getData()
  {
    return $this->data;
  }

  /**
   * Set $this->data
   *
   * @param mixed $data
   * @return Dictionary
   */
  public function setData(&$data)
  {
    $this->data = & $data;
    return $this;
  }

}

?>